package cn.hy.dataSync.alter;


import cn.hy.dataSync.alter.ding.DingTalkAlert;
import cn.hy.dataSync.constant.Delimiter;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;


@Slf4j
@Component
public final class AlertManager {

    private static AlertProperties alertProperties;

    private static DingTalkAlert dingTalkAlert;

    private static String envCluster;

    @Resource
    private void setAlertProperties(AlertProperties alertProperties) {
        AlertManager.alertProperties = alertProperties;
        AlertManager.envCluster = "test";
        //AlertManager.envCluster = System.getProperty("apollo.cluster");
    }

    @Resource
    private void setDingTalkAlert(DingTalkAlert dingTalkAlert) {
        AlertManager.dingTalkAlert = dingTalkAlert;
    }

    public static void sendText(@NotNull String text) {
        sendText(text, alertProperties.getManagerMobiles());
    }

    public static void sendLink(@NotNull String messageUrl,
                                String picUrl,
                                @NotNull String title,
                                @NotNull String text) {
        sendLink(messageUrl, picUrl, title, text, alertProperties.getManagerMobiles());
    }

    public static void sendMarkDown(@NotNull String title,
                                    @NotNull String text) {
        sendMarkDown(title, text, alertProperties.getManagerMobiles());
    }

    public static void sendText(@NotNull String text,
                                final List<String> atMobiles) {
        DingTalkAlert.At at = DingTalkAlert.At.builder().build();
        if (CollectionUtils.isNotEmpty(atMobiles)) {
            at.setAtMobiles(atMobiles);
        } else {
            at.setIsAtAll(true);
        }
        dingTalkAlert.send(
                alertProperties.getManagerDingUrl(),
                alertProperties.getManagerDingSecret(),
                DingTalkAlert.Text.builder()
                        .text(DingTalkAlert.Text.Detail.builder()
                                .content(envCluster +
                                        Delimiter.LINE_BREAK +
                                        text
                                )
                                .build())
                        .at(at)
                        .build()
        );
    }

    public static void sendLink(@NotNull String messageUrl,
                                 String picUrl,
                                @NotNull String title,
                                @NotNull String text,
                                final List<String> atMobiles) {
        DingTalkAlert.At at = DingTalkAlert.At.builder().build();
        if (CollectionUtils.isNotEmpty(atMobiles)) {
            at.setAtMobiles(atMobiles);
        } else {
            at.setIsAtAll(true);
        }
        dingTalkAlert.send(
                alertProperties.getManagerDingUrl(),
                alertProperties.getManagerDingSecret(),
                DingTalkAlert.Link.builder()
                        .link(DingTalkAlert.Link.Detail.builder()
                                .messageUrl(messageUrl)
                                .picUrl(picUrl)
                                .title(title)
                                .text(envCluster +
                                        Delimiter.LINE_BREAK +
                                        text
                                )
                                .build())
                        .at(at)
                        .build()
        );
    }

    public static void sendMarkDown(@NotNull String title,
                                    @NotNull String text,
                                    final List<String> atMobiles) {
        DingTalkAlert.At at = DingTalkAlert.At.builder().build();
        if (CollectionUtils.isNotEmpty(atMobiles)) {
            at.setAtMobiles(atMobiles);
        } else {
            at.setIsAtAll(true);
        }
        dingTalkAlert.send(
                alertProperties.getManagerDingUrl(),
                alertProperties.getManagerDingSecret(),
                DingTalkAlert.Markdown.builder()
                        .markdown(DingTalkAlert.Markdown.Detail.builder()
                                .title(title)
                                .text("## " +
                                        envCluster +
                                        Delimiter.LINE_BREAK +
                                        text +
                                        Delimiter.LINE_BREAK +
                                        atMobiles()
                                )
                                .build())
                        .at(at)
                        .build()
        );
    }

    private static String atMobiles() {
        return Optional.ofNullable(alertProperties.getManagerMobiles())
                .filter(CollectionUtils::isNotEmpty)
                .map(mobiles -> Delimiter.AT + Joiner.on(Delimiter.AT).join(mobiles))
                .orElse(StringUtils.EMPTY);
    }
}
